package com.jzcn.admin.controller.system;

import com.jzcn.admin.annotation.ServerLog;
import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.common.KeyWordDTO;
import com.jzcn.common.dto.common.LabelValueDTO;
import com.jzcn.common.dto.system.role.RoleDTO;
import com.jzcn.common.dto.system.role.RoleListDTO;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.JzPage;
import com.jzcn.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "Role", tags = "角色管理")
@RequestMapping("/api/role")
public class RoleController extends BaseController {

    @Resource
    SysRoleService sysRoleService;

    @ApiOperation("角色列表")
    @GetMapping("/page")
    @RequiresPermissions("system:role:page")
    @ServerLog(value ="角色列表", code= "system:role:page")
    public ResponseResult<JzPage<RoleListDTO>> page(KeyWordDTO dto) {
        return ResponseResult.success(sysRoleService.listPage(dto));
    }

    @ApiOperation("所有角色")
    @GetMapping("/list")
    public ResponseResult<List<LabelValueDTO>> list() {
        return ResponseResult.success(sysRoleService.listData());
    }

    @ApiOperation("新增角色")
    @PostMapping("/create")
    @RequiresPermissions("system:role:create")
    @ServerLog(value ="新增角色", code= "system:role:create")
    @Transactional
    public ResponseResult<Boolean> create(@RequestBody RoleDTO roleDTO) {
        return ResponseResult.success(sysRoleService.create(roleDTO));
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:role:delete")
    @ServerLog(value ="删除角色", code= "system:role:delete")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id) {
        return ResponseResult.success(sysRoleService.delete(id));
    }

    @ApiOperation("更新角色")
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:role:update")
    @ServerLog(value ="更新用户", code= "system:role:update")
    public ResponseResult<Boolean> update(@PathVariable("id") Integer id, @RequestBody RoleDTO roleDTO) {
        return ResponseResult.success(sysRoleService.update(roleDTO, id));
    }

    @ApiOperation("启用/禁用")
    @PostMapping("/status")
    @RequiresPermissions("system:role:status")
    public ResponseResult<Boolean> status(@RequestParam("id") Integer id, @RequestParam Integer status){
        return ResponseResult.success(sysRoleService.roleStatus(id,status));
    }



}
